﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using DXApplicationTangche.DTO;
using DiaoPaiDaYin;
using DXApplicationTangche.UC.销售记录.service;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.门店下单;
using DXApplicationTangche.UC.下单MTO.Service;

namespace DXApplicationTangche.UC.套号管理.套号下单
{
    public partial class Form电商下单 : DevExpress.XtraEditors.XtraForm
    {
        //model
        private OrderModel orderModel = new OrderModel();
        private 订单预览新Model model;
        //变量
        private String stockInventoryNo;//成衣码
        private String salesType;//销售方式：电商、经销商
        public Form电商下单(String stockInventoryNo,String salesType)
        {
            InitializeComponent();
            this.stockInventoryNo = stockInventoryNo;
            this.salesType = salesType;
            try
            {
                this.model = new 订单预览新Model(stockInventoryNo, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
                return;
            }
            //清理无参考意义数据并查询款式类别
            this.model.buildStyleCategoryDTOAndClear();
            //显示该款式的尺码组
            this.comboBox尺码组.Items.Clear();
            List<String> 尺码组 = new List<string>();
            尺码组 = FunctionHelper.JsonDeserialization<List<String>>(
                        RestCall.httpGetMethod(URL.接口url + "/size/findSizeCdByStyleNameCn?styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn));
            foreach (String str in 尺码组)
            {
                this.comboBox尺码组.Items.Add(str);
            }
            this.initCombobox();
        }
        private void initCombobox()
        {
            this.comboBox省份.DataSource = null;
            try
            {
                List<AreaDetailDTO> areaDetailDTOs省份 = MTO订单Service.getAreaDetail("7");
                this.comboBox省份.DataSource = areaDetailDTOs省份;
                this.comboBox省份.DisplayMember = "name";
            }
            catch { }
            
        }

        private void comboBox省份_TextChanged(object sender, EventArgs e)
        {
            this.comboBox城市.DataSource = null;
            this.comboBox区域.DataSource = null;
            try
            {
                AreaDetailDTO dto省份 = this.comboBox省份.SelectedItem as AreaDetailDTO;
                List<AreaDetailDTO> areaDetailDTOs城市 = MTO订单Service.getAreaDetail(dto省份.areaId);
                this.comboBox城市.DataSource = areaDetailDTOs城市;
                this.comboBox城市.DisplayMember = "name";
            }
            catch { }
        }

        private void comboBox城市_TextChanged(object sender, EventArgs e)
        {
            this.comboBox区域.DataSource = null;
            try
            {
                AreaDetailDTO dto城市 = this.comboBox城市.SelectedItem as AreaDetailDTO;
                List<AreaDetailDTO> areaDetailDTOs县区 = MTO订单Service.getAreaDetail(dto城市.areaId);
                this.comboBox区域.DataSource = areaDetailDTOs县区;
                this.comboBox区域.DisplayMember = "name";
            }
            catch { }
        }
        private void comboBox尺码组_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.styleSizeCd = "IGS_GROUP_SIZE-" + this.comboBox尺码组.Text.Trim();
            try
            {
                this.model.尺寸 =
                    FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                        RestCall.httpGetMethod(
                            URL.接口url + "/style/generateStyleMeasureItemsByStyleNameCn?SIZE_CD=" + this.model.OrderCommitDTO返回.styleSizeCd
                                        + "&styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn
                                        + "&categoryId=" + this.model.styleCategoryDTO.id
                                    )
                        );
            }
            catch
            {
                MessageBox.Show("无尺寸信息");
            }
        }
        private void richTextBox备注_TextChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.remarks = this.richTextBox备注.Text.Trim();
        }

        private void simpleButton确定_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.comboBox销售渠道.Text))
            {
                MessageBox.Show("请选择销售渠道");
                return;
            }
            if (String.IsNullOrWhiteSpace(this.textBox收件人.Text)
                    || String.IsNullOrWhiteSpace(this.textBox电话.Text)
                        || String.IsNullOrWhiteSpace(this.comboBox省份.Text)
                            || String.IsNullOrWhiteSpace(this.comboBox城市.Text)
                                || String.IsNullOrWhiteSpace(this.comboBox区域.Text)
                                    || String.IsNullOrWhiteSpace(this.richTextBox详细地址.Text)
                                        )
            {
                MessageBox.Show("请将客户信息填写完整");
                return;
            }
            if (String.IsNullOrEmpty(this.comboBox尺码组.Text))
            {
                MessageBox.Show("选择款式尺寸");
                return;
            }
            DialogResult dialogResult = MessageBox.Show("确认下单吗？", "下单", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                //给orderCommitDTO参考样衣条码尺寸数量及地址赋值
                this.model.OrderCommitDTO返回.orderNumber = Convert.ToDouble(this.numericUpDown数量.Value.ToString());
                this.model.buildOrderCommitDTOSample(this.stockInventoryNo)
                          .buildSaleSampleOrderCommitDTO()
                          .build电商下单(
                            this.textBox收件人.Text.Trim(),
                            this.textBox电话.Text.Trim(),
                            this.comboBox省份.Text,
                            this.comboBox城市.Text,
                            this.comboBox区域.Text,
                            this.richTextBox详细地址.Text.Trim()
                            )
                          .build销售渠道(
                            this.salesType,
                            this.comboBox销售渠道.Text.Trim()
                            );
                this.orderModel.buildAddSaleSampleOrderCommitDto(this.model.OrderCommitDTO返回);
                销售记录Service.post销售记录(
                    new DTO销售记录().build套号(orderModel.orderCommitDTO返回s),
                    Enum订单类别.套号订单
                );
                MessageBox.Show("保存成功");
                this.Close();
            }
        }
    }
}